﻿using System;
using System.IO;
using System.IO.Compression;
using System.Web;
using System.Web.Routing;
using System.Web.UI;

namespace FrontEnd
{
    public class Global : HttpApplication
    {
        private void RegisterRoutes(RouteCollection routes)
        {
            routes.MapPageRoute("Default", // Route name
                                "Home", // Route URL
                                "~/forms/Default.aspx" // Web page to handle route
                );
            routes.MapPageRoute("General", // Route name
                                "General", // Route URL
                                "~/forms/General.aspx" // Web page to handle route
                );
            routes.MapPageRoute("Achievements", // Route name
                                "Achievements", // Route URL
                                "~/forms/Achievements.aspx" // Web page to handle route
                );
            routes.MapPageRoute("Professions", // Route name
                                "Professions", // Route URL
                                "~/forms/Professions.aspx" // Web page to handle route
                );
            routes.MapPageRoute("Reputation/Titles", // Route name
                                "Reputation", // Route URL
                                "~/forms/Reputation.aspx" // Web page to handle route
                );
            routes.MapPageRoute("Items", // Route name
                                "Items", // Route URL
                                "~/forms/Items.aspx" // Web page to handle route
                );
            routes.MapPageRoute("Raid progression", // Route name
                                "RaidProgression", // Route URL
                                "~/forms/RaidProgression.aspx" // Web page to handle route
                );
        }

        protected void Application_Start(object sender, EventArgs e)
        {
            RegisterRoutes(RouteTable.Routes);
        }

        private void Application_PreRequestHandlerExecute(object sender, EventArgs e)
        {
            var app = sender as HttpApplication;
            if (app != null)
            {
                string acceptEncoding = app.Request.Headers["Accept-Encoding"];
                Stream prevUncompressedStream = app.Response.Filter;

                if (!(app.Context.CurrentHandler is Page || app.Context.CurrentHandler.GetType().Name == "SyncSessionlessHandler") || app.Request["HTTP_X_MICROSOFTAJAX"] != null)
                    return;

                if (string.IsNullOrEmpty(acceptEncoding))
                    return;

                acceptEncoding = acceptEncoding.ToLower();

                if (acceptEncoding.Contains("deflate") || acceptEncoding == "*")
                {
                    // defalte
                    app.Response.Filter = new DeflateStream(prevUncompressedStream, CompressionMode.Compress);
                    app.Response.AppendHeader("Content-Encoding", "deflate");
                }
                else if (acceptEncoding.Contains("gzip"))
                {
                    // gzip
                    app.Response.Filter = new GZipStream(prevUncompressedStream, CompressionMode.Compress);
                    app.Response.AppendHeader("Content-Encoding", "gzip");
                }
            }
        }
    }
}